//
//  ShowVideoViewController.m
//  WXXNews
//
//  Created by WangXiaoXiang on 13-6-28.
//  Copyright (c) 2013年 WangXiaoXiang. All rights reserved.
//

#import "ShowVideoViewController.h"

@interface ShowVideoViewController ()

@end

@implementation ShowVideoViewController

- (void)dealloc
{
    [_videoUrl release];
    [_playBar release];
    [_playButton release];
    [_pauseButton release];
    [_stopButton release];
    [_slider release];
    [_videoUrl release];
    [_playerView release];
    [_player release];
    [super dealloc];
}
-(id)initWithVideoURL:(NSString *)url{
    self = [super init];
    if (self) {
        _videoUrl = url;
        NSLog(@"_videoUrl:%@",_videoUrl);
    }
    return self;
}
-(void)makeView{
    //背景设置
    self.view.frame = CGRectMake(0, 0, 320, self.view.bounds.size.height-44);
    self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    //播放条
    _playBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 270, 70)];
    _playBar.center = CGPointMake(160, self.view.bounds.size.height-100);
    _playBar.backgroundColor = [UIColor colorWithRed:0.5 green:0 blue:0 alpha:0.4];
//    _playBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"playBar.png"]];
    [self.view addSubview:_playBar];
    //播放按钮
    _playButton = [CreateButton createCustomButton:[UIImage imageNamed:@"player_play.png"] andWithSEL:@selector(playButtonClicked) andTarget:self andWithFrame:CGRectMake(0, 0, 25, 25)];
    _playButton.center = CGPointMake(kCenter_X,kCenter_Y+kButton_S);
    [_playBar addSubview:_playButton];
    //暂停
    _pauseButton = [CreateButton createCustomButton:[UIImage imageNamed:@"player_pause.png"] andWithSEL:@selector(pauseButtonClicked) andTarget:self andWithFrame:CGRectMake(0, 0, 25, 25)];
    _pauseButton.center = CGPointMake(kCenter_X, kCenter_Y+kButton_S);
    _pauseButton.hidden = YES;
    [_playBar addSubview:_pauseButton];
    //停止
    _stopButton = [CreateButton createCustomButton:[UIImage imageNamed:@"player_stop.png"] andWithSEL:@selector(stopButtonClicked) andTarget:self   andWithFrame:CGRectMake(0, 0, 25, 25)];
    _stopButton.center = CGPointMake(kCenter_X+50, kCenter_Y+kButton_S);
    [_playBar addSubview:_stopButton];
    //返回
    _backButton = [CreateButton createCustomButton:[UIImage imageNamed:@"player_back.png"] andWithSEL:@selector(backButtonClicked) andTarget:self andWithFrame:CGRectMake(0, 0, 25, 25)];
    _backButton.center = CGPointMake(kCenter_X-50, kCenter_Y+kButton_S);
    [_playBar addSubview:_backButton];
    //进度条
    _slider = [[UISlider alloc]initWithFrame:CGRectMake(35, 20, 200, 20)];
    _slider.maximumValue = 1.0f;
    _slider.minimumValue = 0.0f;
    _slider.value = 0.0f;
    [_slider addTarget:self action:@selector(setPro) forControlEvents:UIControlEventValueChanged];
    [_playBar addSubview:_slider];
    [_slider release];
    //创建播放界面
    _playerView = [[PlayerView alloc]initWithFrame:CGRectMake(0, 0, 320, 210)];
    _playerView.backgroundColor = [UIColor blackColor];
    _playerView.center = CGPointMake(160, 150);
    [self.view addSubview:_playerView];
    [_playerView release];
    [self playButtonClicked];
}
//进度改变
-(void)setPro{
    CMTime duration = _player.currentItem.currentTime;
    float pro = _slider.value;
    
    CMTime currentTime = CMTimeMultiply(duration, pro);
    //设置进度
    [_player seekToTime:currentTime];
}
//播放
-(void)playButtonClicked{
    if (_player) {
        [_player play];
    }else{
        
        
        
        AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:_videoUrl] options:nil];
        //读取
        NSLog(@"播放");
        [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
            //得到状态
            AVKeyValueStatus status = [asset statusOfValueForKey:@"tracks" error:nil];
            if (status == AVKeyValueStatusLoaded) {
                
                AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
                _player = [[AVPlayer alloc]initWithPlayerItem:playerItem];
                [_playerView setPlayer:_player];
                [_player play];
                //进度条
                [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_global_queue(0, 0) usingBlock:^(CMTime time){
                    //当前时间
                    CMTime currentTime = _player.currentItem.currentTime;
                    //总时间
                    CMTime duration = _player.currentItem.duration;
                    //进度
                    float proGress = CMTimeGetSeconds(currentTime)/CMTimeGetSeconds(duration);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [_slider setValue:proGress animated:YES];
                    });
                }];
            }
        }];
    }
    
    _playButton.hidden = YES;
    _pauseButton.hidden = NO;
    isPlaying = !isPlaying;
}
//暂停
-(void)pauseButtonClicked{
    if (isPlaying) {
        NSLog(@"暂停");
        _pauseButton.hidden = YES;
        _playButton.hidden = NO;
        [_player pause];
    }
    isPlaying = !isPlaying;
}
//停止
-(void)stopButtonClicked{
    NSLog(@"停止");
    CMTime duration = _player.currentItem.currentTime;
    
    CMTime currentTime = CMTimeMultiply(duration, 0);
    //设置进度
    [_player seekToTime:currentTime];
    
    [self pauseButtonClicked];
}
//放回
-(void)backButtonClicked{
    NSLog(@"返回");
    [self stopButtonClicked];
    if ([self.delegate respondsToSelector:@selector(showVideoVCDismiss:)]) {
        [self.delegate showVideoVCDismiss:self];
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self makeView];
    //继续在这写
    
	// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
